<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>AUnit: Fake Arduino Classes</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">AUnit
   &#160;<span id="projectnumber">1.7.1</span>
   </div>
   <div id="projectbrief">Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="PageDoc"><div class="header">
  <div class="headertitle">
<div class="title">Fake Arduino Classes </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>This directory contains fake versions of some Arduino classes for the purposes of unit testing. These header files are <em>not</em> included automatically by the <code>#include &lt;<a class="el" href="AUnit_8h.html" title="Same as AUnitVerbose.h except that the terse versions of the various assertXxx() macros are provided.">AUnit.h</a>&gt;</code> preprocessor directive. The various fake classes must be included manually just after the <code>&lt;<a class="el" href="AUnit_8h.html" title="Same as AUnitVerbose.h except that the terse versions of the various assertXxx() macros are provided.">AUnit.h</a>&gt;</code> is included. The fake classes live in the <code>aunit::fake</code> namespace.</p>
<h1>FakePrint</h1>
<p>The <code>FakePrint</code> class is an implementation of the <a href="https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/Print.h">Print</a> class which is the base class of the <a href="https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/HardwareSerial.h">HardwareSerial</a> and other output classes. The <code>Serial</code> global object is an instance of the <code>HardwareSerial</code> class. If a user-defined class or method is defined to use an instance of the <code>Print</code> object, instead of hardcoding a dependency to the <code>Serial</code> instance, then an instance of the <code>FakePrint</code> class can be substituted and used to write unit tests for the user-defined class or method.</p>
<p>Let's say the user-defined class is called <code>Greeter</code> and a method called <code>greet()</code> prints something out to <code>Serial</code>:</p>
<div class="fragment"><div class="line"> {C++}</div>
<div class="line">class Greeter {</div>
<div class="line">  public:</div>
<div class="line">    Greeter(Print&amp; printer):</div>
<div class="line">      mPrinter(printer) {}</div>
<div class="line"> </div>
<div class="line">    void greet(int n) {</div>
<div class="line">      mPrinter.print(&quot;hello &quot;);</div>
<div class="line">      mPrinter.print(n);</div>
<div class="line">      mPrinter.println(&quot; world(s)&quot;); </div>
<div class="line">    }</div>
<div class="line"> </div>
<div class="line">  private:</div>
<div class="line">    Print&amp; mPrinter;</div>
<div class="line">};</div>
</div><!-- fragment --><p>If we want to verify that the <code>Greeter::greet()</code> method prints what is expected, we inject an instance of <code>FakePrint</code> (instead of <code>Serial</code>) in a test called <code>GreeterTest.ino</code>:</p>
<div class="fragment"><div class="line"> {C++}</div>
<div class="line">#line 2 &quot;GreeterTest.ino&quot;</div>
<div class="line"> </div>
<div class="line">#include &lt;AUnit.h&gt;</div>
<div class="line">#include &lt;aunit/fake/FakePrint.h&gt;</div>
<div class="line"> </div>
<div class="line">using namespace aunit;</div>
<div class="line">using namespace aunit::fake;</div>
<div class="line"> </div>
<div class="line">test(GreeterTest, greet) {</div>
<div class="line">  FakePrint fakePrint;</div>
<div class="line">  Greeter greeter(fakePrint);</div>
<div class="line"> </div>
<div class="line">  greeter.greet(1);</div>
<div class="line">  assertEqual(&quot;hello 1 world(s)\r\n&quot;, fakePrint.getBuffer());</div>
<div class="line">  fakePrint.flush();</div>
<div class="line"> </div>
<div class="line">  greeter.greet(2);</div>
<div class="line">  assertEqual(&quot;hello 2 world(s)\r\n&quot;, fakePrint.getBuffer());</div>
<div class="line">  fakePrint.flush();</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line">void setup() {</div>
<div class="line">  delay(1000);</div>
<div class="line">  Serial.begin(115200);</div>
<div class="line">  while (! Serial); // Wait until Serial is ready - Leonardo/Micro</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line">void loop() {</div>
<div class="line">  TestRunner::run();</div>
<div class="line">}</div>
</div><!-- fragment --> </div></div><!-- contents -->
</div><!-- PageDoc -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.1
</small></address>
</body>
</html>
